<?php

namespace App\Console\Commands;

use App\Models\Base\ServerList;
use App\Models\Base\ServerPlan;
use App\Service\RequestService;
use App\Service\ScriptService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

/**
 * 执行计划开服.
 */
class ServerOpenPlan extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'server:openPlan';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '定时开服';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //待开服列表
        $plans = ServerPlan::whereIn('status', [0, 1])->get();
        if (0 == $plans->count()) {
            return false;
        }
        //当前时间
        $open_time = time();
        foreach ($plans as $server) {
            $time_exp = strtotime($server->open_time) - $open_time;
            if ($time_exp > 1320 && 0 == $server->status) {
                continue;
            }
            if (0 == $server->status) {
                if (1 == $server->is_add) {
                    //加入口不开服
                    $old_server = ServerPlan::where('server_id', $server->server_id - 1)->first();
                    if (!$old_server) {
                        continue;
                    }
                } else {
                    //发送给游戏服
                    if (false == $this->sendGame($server)) {
                        continue;
                    }
                    Log::info('执行计划开服:', ['server_id' => $server->server_id]);
                    //修改合服配置文件
                    $this->merge($server);
                    //执行开服脚本
                    ScriptService::openServer($server->server_id, $server->port);
                    //启动游戏服
                    ScriptService::startGame($server->server_id);
                }
                //修改状态为等待对外
                $server->status = 1;
                $server->save();
                $server->name = 's'.$server->server_id.$server->name;
                //写入本地表
                $this->insert($server);
            } elseif (1 == $server->status && strtotime($server->open_time) <= $open_time) {
                //对外
                Log::info('计划开服对外:', ['server_id' => $server->server_id]);
                RequestService::changeState($server->server_id, config('game.state')[0]);
                $this->update($server->server_id);
                //修改状态为完成
                $server->status = 2;
                $server->save();
            }
        }
    }

    /**
     * [update 修改服务器状态].
     *
     * @Author   王文凡
     * @DateTime 2018-03-26
     *
     * @version  1.0
     *
     * @param int $server_id [服务器id]
     *
     * @return
     */
    protected function update($server_id)
    {
        $status = config('game.state')[0];
        ServerList::where('server_id', $server_id)->update(['status' => $status]);
    }

    /**
     * [insert 新增服务器到本地].
     *
     * @Author   王文凡
     * @DateTime 2018-03-26
     *
     * @version  1.0
     *
     * @param object $model [description]
     *
     * @return
     */
    protected function insert($model)
    {
        $list = new ServerList();
        $list->server_id = $model->server_id;
        $list->name = $model->name;
        $list->port = $model->port;
        $list->reg_id = $model->reg_id;
        $list->status = config('game.state')[5];
        $list->ip = config('game.server_ip');
        $ret = $list->save();
        if (!$ret) {
            Log::error('计划开服写入本地失败 :', ['server_id' => $model->server_id]);
        }
    }

    /**
     * [merge 修改合服配置文件].
     *
     * @Author   王文凡
     * @DateTime 2018-03-26
     *
     * @version  1.0
     *
     * @param object $model
     *
     * @return
     */
    protected function merge($model)
    {
        if (empty($model->merge_ids)) {
            return false;
        }
        ScriptService::exePlanMerge($model->server_id, $model->merge_ids);
    }

    /**
     * [sendGame 向游戏服发送开服消息].
     *
     * @Author   王文凡
     * @DateTime 2018-03-26
     *
     * @version  1.0
     *
     * @param [object] $model [开服数据]
     *
     * @return
     */
    protected function sendGame($model)
    {
        Log::info('定时发送开服:', ['server_id' => $model->server_id]);
        $status = config('game.state')[5];
        $data = [
            'server_id' => intval($model->server_id),
            'server_name' => $model->name,
            'server_ip' => config('game.server_ip'),
            'server_port' => intval($model->port),
            'server_status' => intval($status),
            'server_desc' => $model->desc,
            'server_sign' => config('game.server_sign'),
            'region_name' => $model->region->name,
            'plat_limits' => '',
            'open_time' => 0,
        ];

        return RequestService::sendAddMsg($data);
    }
}
